package com.dl.mall.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * rabbitmq配置类
 * @author DL
 * @create 2022-05-07 16:43
 */
@Configuration
public class RabbitmqConfig {

    public static final String DEAD_LETTER_EXCHANGE = "dead.exchange";// 死信交换机
    public static final String DEAD_LETTER_QUEUE = "dead.queue";// 死信队列
    public static final String DEAD_LETTER_ROUTING_KEY = "dead.routingKey";// 死信路由

    public static final String EXCHANGE_NAME = "exchange.name";// 普通交换机
    public static final String QUEUE_A = "queue.a";  // A队列
    public static final String QUEUE_B = "queue.b";    // B队列
    public static final String ROUTING_KEY = "routing_key";// routingKey
    // 声明死信交换机
    @Bean()
    public DirectExchange deadExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }

    //声明死信队列 QD
    @Bean()
    public Queue deadQueue(){
        return new Queue(DEAD_LETTER_QUEUE);
    }

    //声明死信队列 QD 绑定关系
    @Bean
    public Binding deadQueueBindingDeadExchange(@Qualifier("deadQueue") Queue queueD,
                                        @Qualifier("deadExchange") DirectExchange exchange){
        return BindingBuilder.bind(queueD).to(exchange).with(DEAD_LETTER_ROUTING_KEY);
    }

    // 声明普通交换机
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(EXCHANGE_NAME);
    }

    // 队列a
    @Bean
    public Queue queueA() {
        return QueueBuilder
                .durable(QUEUE_A)
                //声明该队列的死信消息发送到的 交换机 （队列添加了这个参数之后会自动与该交换机绑定，并设置路由键，不需要开发者手动设置)
                .withArgument("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE)
                //声明该队列死信消息在交换机的 路由键
                .withArgument("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY)
                .build();
    }

/*    // 队列b
    @Bean
    public Queue queueB() {
        return QueueBuilder
                .durable(QUEUE_B)
                //声明该队列的死信消息发送到的 交换机 （队列添加了这个参数之后会自动与该交换机绑定，并设置路由键，不需要开发者手动设置)
                .withArgument("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE)
                //声明该队列死信消息在交换机的 路由键
                .withArgument("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY)
                .build();
    }*/

    // 队列绑定到确认交换机
    @Bean
    public Binding queueABindingFanoutExchange(@Qualifier("queueA") Queue queue,
                                       @Qualifier("fanoutExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }
/*
    // 队列绑定到确认交换机
    @Bean
    public Binding queueBBindingFanoutExchange(@Qualifier("queueB") Queue queue,
                                              @Qualifier("fanoutExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }*/



}
